'use strict';
const db = uniCloud.database();
exports.main = async (event, context) => {
	//event为客户端上传的参数
	console.log('event : ', event)
	const {
		action,
		params
	} = event;
	let res;
	switch (action) {
		case 'record':
			//     //有同样的类型是 查出来，更新
			//  let sameText = await db.collection('record').where({
			// 	 text:params.text,
			// 	 user_id:params.uni_id
			//  }).get();
			//  let price = sameText.data.reduce((pre,cur) => {
			// 	 return pre += Number(cur.price)
			//  },0)
			// let data = sameText.data[0];
			// price = price + Number(params.price);
			//  if(sameText.affectedDocs){
			// 	 await db.collection('record').doc(data._id).update({
			// 	 	updateTime:Date.now(),
			// 		time:data.time,
			// 	 	price:price.toString(),
			// 	 })
			//  }else{
			// 	await db.collection('record').add({
			// 		date:Date.now(),
			// 		price:params.price,
			// 		type:params.type,
			// 		text:params.text,
			// 		url:params.url,
			// 		user_id:params.uni_id
			// 	}) 
			//  }
			let time = new Date();
			let month = time.getMonth()+1;
			let day = time.getDate();
			let year = time.getFullYear();
			await db.collection('record').add({
				year,
				month,
				day,
				date: Date.now(),
				price: params.price,
				type: params.type,
				text: params.text,
				url: params.url,
				user_id: params.uni_id,
			})

			return res = {
				msg: '记录成功'
			}
			break;
		case 'recordOut':
			let skip = (params.page - 1)*params.pageSize;
			res = await db.collection('record').where({
				type: params.type,
				user_id: params.uid
			}).skip(skip).limit(params.pageSize).orderBy('date', 'desc').get();
			let total = await db.collection('record').where({
				type: params.type,
				user_id: params.uid
			}).count()
			return {
				list:res.data,
				total:total.total
			};
			break;
		case 'del':
			res = await db.collection('record').where({
				user_id: params.user_id,
				_id: params._id
			}).remove()
			return {
				msg: '删除成功'
			}
			break;
		case 'update':
			res = await db.collection('record').where({
				user_id: params.uid,
				_id: params._id
			}).update({
				updateTime: Date.now(),
				price: params.price
			})
			return {
				msg: '更新成功'
			}
			break;
		case 'statistics':
		    let queryParams = {
				user_id: params.uid,
				year:params.year,
				month:params.month
			}
			params.currentDay && (queryParams.day = params.day)
			
			let result = await db.collection('record').where(queryParams).get();
			for(let i= 0;i<result.data.length;i++){
				 for (let k = i + 1; k < result.data.length; k++) {
				        if (result.data[i].text == result.data[k].text) {
				            result.data[i].price = (Number(result.data[i].price) + Number(result.data[k].price)).toString();
				            result.data[k].price = result.data[i].price;
				        }
				    }
			};
			let obj = {};
			result.data = result.data.reduce((pre,cur)=>{
				obj[cur.text] ? "":obj[cur.text] = true && pre.push(cur);
				return pre
			},[])
			return result.data
			break;
			
	}
	//返回数据给客户端
	return res;
};
